package com.teziyou.commodity.logic;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import jodd.bean.BeanUtil;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.mytravel.commodityBridge.service.bean.ScenicPicPath;
import com.mytravel.commodityBridge.service.bean.ScenicSpot;
import com.teziyou.commodity.enumtype.EnableEnum;
import com.teziyou.commodity.service.DictService;
import com.teziyou.commodity.service.SceneService;
import com.teziyou.commodity.util.ConvertUtil;
import com.teziyou.commodity.util.DateUtil;

@SuppressWarnings({"rawtypes", "unchecked"})
@Service
public class SceneLogic {
	
	@Autowired
	private SceneService sceneService;
	@Autowired
	private DictService dictService;

	public List findSceneData(Map<String, String> map) {
		String address = ConvertUtil.toString(map.get("sce_address"));
		String name = ConvertUtil.toString(map.get("name"));
		if(StringUtils.isNotBlank(address)) map.put("sce_address", "%"+address.replaceAll("\\s", "")+"%");
		if(StringUtils.isNotBlank(name)) map.put("name", "%"+name+"%");
		List list = sceneService.findScenesByPage(map);
		if(list!=null) {
			Iterator iter = list.iterator();
			while(iter.hasNext()) {
				Map scene = (Map) iter.next();
				int levelId = ConvertUtil.toInt(scene.get("DictionaryId"));
				scene.put("level", dictService.getDictionaryById(levelId).getName());
				scene.put("IsOK_display", EnableEnum.getEnumByCode(ConvertUtil.toInt(scene.get("IsOK"))).getDesc());
			}
		}
		return list;
	}

	public boolean addScene(Map sceneMap) {
		System.err.println(sceneMap);
		String imgs = ConvertUtil.toString(sceneMap.get("imgs"));
		if(StringUtils.isNotBlank(imgs)) {
			String[] imgStrs = imgs.split(",");
			/*List<String> imgList = Arrays.asList(imgStrs);
			String mainpic = sceneMap.get("mainpic");
			if(StringUtils.isBlank(mainpic)) mainpic = imgList.get(0); 
			imgList.remove(mainpic);
			sceneMap.put()*/
			List<ScenicPicPath> spps = new ArrayList<>();
			String mainpic = ConvertUtil.toString(sceneMap.get("mainpic"));
			for(int i=0;i<imgStrs.length;i++) {
				String imgStr = imgStrs[i];
				if(i==0&&StringUtils.isBlank(mainpic)) {
					mainpic = imgStr;
					continue;
				} else if(imgStr.equals(mainpic)) {
					continue;
				}
				ScenicPicPath spp = new ScenicPicPath();
				spp.setUrlPath(imgStr);
				spps.add(spp);
			}
			sceneMap.put("scenicPicPathList", spps);
		}
		sceneMap.remove("imgs");
		sceneMap.remove("mainpic");
		
		ScenicSpot ss = new ScenicSpot();
		//new Beans(sceneMap).fillBean(ss);
//		ss.admDivId = ConvertUtil.toInt(sceneMap.get("admDivId"));
//		ss.bestTravelTime = sceneMap.get("bestTravelTime");
//		ss.caution = sceneMap.get("caution");
		BeanUtil.populateBean(ss, sceneMap);
		System.out.println(sceneMap);
		System.out.println(ss.getDescription());
		System.out.println(ss.getCaution());
		System.out.println(ss.getTrafficInfo());
		return sceneService.addScene(ss);
	}

	public boolean deleteSceneById(long id) {
		return sceneService.deleteSceneById(id);
	}

	public boolean updateScene(ScenicSpot scene) {
		return sceneService.updateScene(scene);
	}

	public List findScenesByPage(Map<String, String> condition) {
		return sceneService.findScenesByPage(condition);
	}

	public int findScenesCount(Map<String, String> condition) {
		return sceneService.findScenesCount(condition);
	}

	public Map findSceneById(long id) {
		Map map = sceneService.findSceneById(id);
		map.put("open_time", DateUtil.formatDate(new Date(ConvertUtil.toLong(map.get("open_time"))), "yyyy-MM-dd HH:mm:ss"));
		map.put("close_time", DateUtil.formatDate(new Date(ConvertUtil.toLong(map.get("close_time"))), "yyyy-MM-dd HH:mm:ss"));
		return map;
	}
	
	

}
